/**
 * Created by wangcan on 2017-10-26.
 */
var moment = require('moment');
var messageUtil = require('../../common/utils/messageUtil');
var mtOrderService = require('../../services/mtOrderService');
var orderTotalCommisionService = require('../../services/commission/orderTotalCommisionService');
var nodeExcel = require('excel-export');
var toolUtil = require('../../common/utils/toolUtil');

// 查询订单总佣金
exports.orderTatolCommisionList = function(req, res) {
    var body = req.body;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;

    orderTotalCommisionService.orderTatolCommisionList(body, size, index, function(err, results) {
        if (err) {
            logger.error('Exception happen when orderCommisionList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when orderCommisionList...'));
        }else {
            var orderDataRes = results[1];
            var orderData = [];
            if (orderDataRes != null && orderDataRes.length > 0) {
                for (var i = orderDataRes.length - 1; i >= 0; i--) {
                    orderData.push(orderDataRes[i]);
                }
            }
            var data = {
                count: results[0]["count"],
                totalPage: results[0]["page"],
                list: orderData
                // list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }

    });
};
// 导出订单总佣金
exports.orderTotalCommisionDataExport = function (req,res){
    var body = req.cparams;
    var size = -1;
    var index = -1;
    orderTotalCommisionService.orderTatolCommisionList(body, size, index, function(err, results) {
        if (err) {
            logger.error('Exception happen when exportOrderCommisionList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when exportOrderCommisionList...'));
        } else {
            if(!results[1]) {
                logger.error('Exception happen when exportOrderCommisionList...导出数据为0' + err.message);
                res.json(messageUtil.composeSucceededMessage(req, null, {}));
            }else {
                var conf={};
                conf.cols= [{
                    caption: '下单日期',
                    type: 'string',
                    width:25.0
                },{
                    caption: '已成团订单数',
                    type: 'string',
                    width:25.0
                },{
                    caption: '已退款订单数',
                    type: 'string',
                    width:25.0
                },{
                    caption: '所得佣金',
                    type: 'string',
                    width:25.0
                },{
                    caption: '帖子发布数',
                    type: 'string',
                    width:25.0
                },{
                    caption: '闲置发布数',
                    type: 'string',
                    width:25.0
                },{
                    caption: '邀请用户数',
                    type: 'string',
                    width:25.0
                }];
                var orderCommissionObjArr = results[1];
                // var data = [];
                var tempRow = [];
                if (orderCommissionObjArr != null && orderCommissionObjArr.length > 0) {
                    for (var i = orderCommissionObjArr.length - 1; i >= 0; i--) {
                        var temp = [];
                        if (orderCommissionObjArr[i].orderCountDate) {
                            temp.push(orderCommissionObjArr[i].orderCountDate);
                        } else {
                            temp.push("");
                        }
                        temp.push(orderCommissionObjArr[i].groupOrders.toString());
                        temp.push(orderCommissionObjArr[i].refundOrders.toString());
                        temp.push(orderCommissionObjArr[i].commissionAll.toString());
                        temp.push(orderCommissionObjArr[i].postPushCounts.toString());
                        temp.push(orderCommissionObjArr[i].goodsPushCounts.toString());
                        temp.push(orderCommissionObjArr[i].invitedUsers.toString());

                        tempRow.push(temp);
                    }
                }
                var body = req.cparams;
                var startDateTemp = moment(body.startdate.substring(0,10)).format('YYYYMMDD').toString();
                var endDateTemp = moment(body.enddate.substring(0,10)).format('YYYYMMDD').toString();
                var filename = "OrderCommission_" + startDateTemp + "-".toString() + endDateTemp + ".xlsx";
                conf.rows = tempRow;
                var ret = nodeExcel.execute(conf);
                res.setHeader('Content-Type', 'application/vnd.openxmlformats');
                res.setHeader("Content-Disposition", "attachment; filename=" + filename);
                res.end(ret, 'binary');
            }
        }
    })
}
